package cn.tedu.csms.product.service.impl;



import cn.tedu.csms.product.mapper.ProductMapper;
import cn.tedu.csms.product.pojo.dto.DeleteParam;
import cn.tedu.csms.product.pojo.dto.ProductQuery;
import cn.tedu.csms.product.pojo.dto.ProductSaveParam;
import cn.tedu.csms.product.pojo.entity.Product;
import cn.tedu.csms.product.pojo.entity.Storage;
import cn.tedu.csms.product.pojo.vo.ProductVO;
import cn.tedu.csms.product.service.ProductService;
import cn.tedu.csms.storage.mapper.StorageMapper;
import cn.tedu.csms.storage.service.StorageService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Transactional
@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
     ProductMapper productMapper;
    @Autowired
    StorageMapper storageMapper;


    @Override
    public List<ProductVO> selectProduct(ProductQuery productQuery) {
        log.debug("查询业务，参数{}", productQuery);
        List<ProductVO> list = productMapper.selectProduct(productQuery);
        return list;
    }











    @Override
    public void saveProduct(ProductSaveParam productSaveParam) {
        log.debug("保存业务，参数1{},参数2{}", productSaveParam);
        Product product = new Product();
        Storage storage = new Storage();
        BeanUtils.copyProperties(productSaveParam, product);
        BeanUtils.copyProperties(productSaveParam, storage);
        log.debug("产品的参数，参数{}", product);
        log.debug("库存的参数，参数{}", storage);


        if (product.getId() == null ){
            log.debug("新增路线");
            product.setCreateTime(new Date());
            storage.setCreateTime(new Date());
            productMapper.saveProduct(product);
            log.debug("查看产品ID：{}", product.getId());
            storage.setGoodsId(product.getId());
            log.debug("保存库存{}", storage);
//            storageMapper.saveStorage(storage);

        }else {
            log.debug("更新路线");
            storage.setGoodsId(product.getId());
            product.setUpdateTime(new Date());
            storage.setUpdateTime(new Date());
            productMapper.updateProduct(product);
            storageMapper.updateStorage(storage);

        }

    }


    @Autowired
    StorageService  storageService;
    @Override
    public void deleteProduct(DeleteParam deleteParam) {
        log.debug("删除业务，参数{}", deleteParam);

        productMapper.deleteProduct(deleteParam);
        storageService.deleteStorage(deleteParam);

    }
}
